package sg.com.techstudio.ecc.validator;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import sg.com.techstudio.ecc.dao.RegisterDao;
import sg.com.techstudio.ecc.obj.ParticipantObj;
import sg.com.techstudio.ecc.obj.RegisterObj;

@Component
public class ParticipantObjValidator extends BaseValidator {
	private static final Log log = LogFactory.getLog(ParticipantObjValidator.class);

	@Autowired
	private RegisterDao registerDao;
	
	public void validateParticipantList(RegisterObj r, ValidationContext context) {
		log.debug("validateParticipantList() entry");
		MessageContext messages = context.getMessageContext();
		
		List<ParticipantObj> pList = r.getParticipantList();
		if(pList==null)
			return;
		int i=0;
		for(ParticipantObj p: pList) {
			if(StringUtils.isBlank(p.getSalutation()))
				addMessage(participantListFieldPath("salutation", i), "required.selectfield", messages);
			validateEmptyString(p.getName(), participantListFieldPath("name", i), messages);
			if(!NRIC_PATTERN.matcher(p.getNric()).matches() || !validateSGNric(p.getNric()))
				addMessage(participantListFieldPath("nric", i), "invalid.nric", messages);
			else if(validateNricDuplicate(p.getNric(), r.getParticipantList(), r.getNric(), i))
				addMessage(participantListFieldPath("nric", i), "nric.duplicate", messages);
			if(validateEmptyString(p.getBadgeName(), participantListFieldPath("badgeName", i), messages))
				if(p.getBadgeName().length()>20)
					p.setBadgeName(StringUtils.substring(p.getBadgeName(), 0, 20));
			validateEmptyString(p.getDesignation(), participantListFieldPath("designation", i), messages);
			if(validateEmptyString(p.getMobile(), participantListFieldPath("mobile", i), messages))
				if(!MOBILE_PATTERN.matcher(p.getMobile()).matches())
					addMessage(participantListFieldPath("mobile", i), "invalid.mobile", messages);
			validateEmail(p.getEmail(), participantListFieldPath("email", i), messages);
			if(p.getSessionA()==null)
				addMessage(participantListFieldPath("sessionA", i), "required.session", messages);
			if(p.getSessionB()==null)
				addMessage(participantListFieldPath("sessionB", i), "required.session", messages);
			if(p.getSessionC()==null)
				addMessage(participantListFieldPath("sessionC", i), "required.session", messages);
			if(p.getSessionD()==null)
				addMessage(participantListFieldPath("sessionD", i), "required.session", messages);
			if(p.getVegetarian()==null)
				addMessage(participantListFieldPath("vegetarian", i), "required.selectfield", messages);
			i++;
		}
	}
	
	
	protected static final String PARTICIPANTLIST_PATH = "participantList";
	public String participantListFieldPath(String fieldname, int i) {
		return PARTICIPANTLIST_PATH+"["+i+"]."+fieldname;
	}
	
	protected boolean validateNricDuplicate(String nric, List<ParticipantObj> participantList, String mainNric, int i) {
		if(StringUtils.equalsIgnoreCase(nric, mainNric))
			return true;
		if(registerDao.isNricExist(nric, 0))
			return true;
		if(participantList==null || participantList.isEmpty())
			return false;
		int j=0;
		for(ParticipantObj p: participantList) {
			if(i==j)
				continue;
			if(StringUtils.equalsIgnoreCase(nric, p.getNric()))
				return true;
			j++;
		}
		return false;
	}
}
